package com.hyj.ecp.backstage.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;

import com.ecp.ResultObject;
import com.hyj.ecp.backstage.sys.bean.qo.LoginQO;
import com.hyj.ecp.backstage.sys.service.IRoleService;
import com.hyj.ecp.common.dao.IjdbcDAO;
@Service
public class RoleService implements IRoleService {

	@Resource(name="jdbcDao")
    private  IjdbcDAO dao;

	@Override
	public Map<String, Object> queryBackUser(LoginQO qo) {
		List<Object> param = new ArrayList<Object>();
		String sql = "  select  id,`account`, `name`, `phone`,  `mobile`,  `status`, " +
				"    `createperson`, `createtime`, `modperson`,  `modtime`," +
				"   IFNULL((SELECT r.NAME FROM  t_back_role r WHERE t.ROLEID= r.id),'') AS rolename" +
				"    from  `t_back_user`  t where 1=1   " ;
				 
				 
		if(qo.getAccount()!=null&&qo.getAccount().length()>0){
			sql= sql+" and t.account like ? ";
			param.add("%"+qo.getAccount()+"%");
		}
		
		if(qo.getName()!=null&&qo.getName().length()>0){
			sql= sql+" and t.name like ? ";
			param.add("%"+qo.getName()+"%");
		}
		
		ResultObject result = dao.pageQuery(sql, qo.getCurPage(), qo.getSize(),  param.toArray());
		Map<String,Object> vo = new HashMap<String,Object>();
    	vo.put("rowTotal", result.getRowCount());
    	vo.put("infoContent", result.getRows());
		return  vo;
	}

	@Override
	public int saveUser(LoginQO qo) {
		String sql = " INSERT INTO  `t_back_user` " + 
				"            (" + 
				"             `ACCOUNT`, " + 
				"             `PASSWORD`," + 
				"             `NAME`, ROLEID ," + 
				"             `PHONE`, " + 
				"             `MOBILE`, " + 
				"             `REMARK`, " + 
				"             `STATUS`, " + 
				"             `CREATEPERSON`, " + 
				"             `CREATETIME`) " + 
				"VALUES ( \r\n" + 
				"        ?,\r\n" + 
				"        ?,\r\n" + 
				"        ?,?,\r\n" + 
				"        ?,\r\n" + 
				"        ?,\r\n" + 
				"        ?,\r\n" + 
				"        ?,\r\n" + 
				"        ?,\r\n" + 
				"        NOW()\r\n" + 
				"        )  " ;
		
		 return dao.update(sql,new Object[]{qo.getAccount(),qo.getPassword(),qo.getName(),qo.getRoleId(),
				               qo.getPhone(),qo.getMobile(),qo.getRemark(),"1",qo.getOptUser()});
	}

	@Override
	public int updateUser(LoginQO qo) {
		String sql = "  UPDATE `ecp`.`t_back_user`\r\n" + 
				"SET  \r\n" + 
				"  `ACCOUNT` = ?  ,\r\n" + 
				"  `PASSWORD` = ? ,\r\n" + 
				"  `NAME` = ?,\r\n" + 
				"  `PHONE` = ?,\r\n" + 
				"  `MOBILE` = ?,\r\n" + 
				"  `REMARK` = ?,\r\n" + 
				"  `MODPERSON` = ?,ROLEID = ? ,   \r\n" + 
				"  `MODTIME` = NOW() \r\n" + 
				"WHERE `ID` = ? ";
		return dao.update(sql, new Object[]{qo.getAccount(),qo.getPassword(),qo.getName(),qo.getPhone(),qo.getMobile()
				          ,qo.getRemark(),qo.getOptUser(),qo.getRoleId(),qo.getId()});
	}

	@Override
	public Map<String, Object> queryBackSingleUser(LoginQO qo) {
		String sql = "  SELECT * FROM t_back_user t WHERE t.ID = ? ";
		
		return dao.queryForMap(sql, new Object[]{qo.getId() });
	}

	@Override
	public List<Map<String, Object>> queryRole(LoginQO qo) {
		String sql = "  SELECT t.ID, t.NAME FROM  t_back_role t  WHERE t.STATUS ='1' " ; 
		return dao.queryForList(sql,new Object[]{});
	}
	
}
